package com.tensquare.recruit.service;

import com.tensquare.recruit.dao.RecruitDao;
import com.tensquare.recruit.pojo.Recruit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RecruitService {
    @Autowired
    private RecruitDao recruitDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     */

    public List<Recruit> findAll(){
        return recruitDao.findAll();
    }

    /**
     * 条件查询 不带分页
     */

    public List<Recruit> findSearch(Map whereMap){
        Specification<Recruit> specification = createSpecification(whereMap);
        return recruitDao.findAll(specification);
    }

    //根据ID查询
    public Object findByid(String id) {
        return recruitDao.findById(id).get();
    }

    /**
     * 条件查询+分页
     */
     public Page<Recruit> findSearch(Map whereMap,int page,int size){
         Specification<Recruit> specification = createSpecification(whereMap);
         PageRequest pageRequest = PageRequest.of(page-1,size);
         return recruitDao.findAll(specification,pageRequest);
     }


     //增
    public void add(Recruit recruit){
         recruit.setId(idWorker.nextId()+"");
         recruitDao.save(recruit);
    }
     //删
    public void deleteById(String id){
         recruitDao.deleteById(id);
    }
    //改
    public void update(Recruit recruit){
         recruitDao.save(recruit);
    }

    /**
     * 动态条件构建
     *
     *
     * if (null != searchMap.get("")&& !"".equals(searchMap.get(""))){
     *                     predicateList.add(cb.like(root.get("").as(String.class),"%"+(String)searchMap.get("")+"%"));
     *                 }
     */

    private Specification<Recruit> createSpecification(Map searchMap){
        return new Specification<Recruit>() {
            @Override
            public Predicate toPredicate(Root<Recruit> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                //ID
                if (null != searchMap.get("id")&& !"".equals(searchMap.get("id"))){
                    predicateList.add(cb.like(root.get("id").as(String.class),"%"+(String)searchMap.get("id")+"%"));
                }
                //职位名称
                if (null != searchMap.get("jobname")&& !"".equals(searchMap.get("jobname"))){
                    predicateList.add(cb.like(root.get("jobname").as(String.class),"%"+(String)searchMap.get("jobname")+"%"));
                }
                //薪资范围
                if (null != searchMap.get("salary")&& !"".equals(searchMap.get("salary"))){
                    predicateList.add(cb.like(root.get("salary").as(String.class),"%"+(String)searchMap.get("salary")+"%"));
                }
                //经验要求
                if (null != searchMap.get("condition")&& !"".equals(searchMap.get("condition"))){
                    predicateList.add(cb.like(root.get("condition").as(String.class),"%"+(String)searchMap.get("condition")+"%"));
                }
                //学历要求
                if (null != searchMap.get("education")&& !"".equals(searchMap.get("education"))){
                    predicateList.add(cb.like(root.get("education").as(String.class),"%"+(String)searchMap.get("education")+"%"));
                }
                //任职方式
                if (null != searchMap.get("type")&& !"".equals(searchMap.get("type"))){
                    predicateList.add(cb.like(root.get("type").as(String.class),"%"+(String)searchMap.get("type")+"%"));
                }
                //办公地址
                if (null != searchMap.get("address")&& !"".equals(searchMap.get("address"))){
                    predicateList.add(cb.like(root.get("address").as(String.class),"%"+(String)searchMap.get("address")+"%"));
                }
                //企业ID
                if (null != searchMap.get("eid")&& !"".equals(searchMap.get("eid"))){
                    predicateList.add(cb.like(root.get("eid").as(String.class),"%"+(String)searchMap.get("eid")+"%"));
                }
                //状态
                if (null != searchMap.get("state")&& !"".equals(searchMap.get("state"))){
                    predicateList.add(cb.like(root.get("state").as(String.class),"%"+(String)searchMap.get("state")+"%"));
                }
                //网址
                if (null != searchMap.get("url")&& !"".equals(searchMap.get("url"))){
                    predicateList.add(cb.like(root.get("url").as(String.class),"%"+(String)searchMap.get("url")+"%"));
                }
                //标签
                if (null != searchMap.get("label")&& !"".equals(searchMap.get("label"))){
                    predicateList.add(cb.like(root.get("label").as(String.class),"%"+(String)searchMap.get("label")+"%"));
                }
                //职位描述
                if (null != searchMap.get("content1")&& !"".equals(searchMap.get("content1"))){
                    predicateList.add(cb.like(root.get("content1").as(String.class),"%"+(String)searchMap.get("content1")+"%"));
                }
                //职位要求
                if (null != searchMap.get("content2")&& !"".equals(searchMap.get("content2"))){
                    predicateList.add(cb.like(root.get("content2").as(String.class),"%"+(String)searchMap.get("content2")+"%"));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }


}
